package com.tms.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tms.basic.pojo.domain.Role;
import com.tms.basic.mapper.RoleMapper;
import com.tms.basic.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public Page<Role> listRoles(Page<Role> page, Role role) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(role.getRoleCode()), Role::getRoleCode, role.getRoleCode())
              .like(StringUtils.isNotBlank(role.getRoleName()), Role::getRoleName, role.getRoleName())
              .eq(StringUtils.isNotBlank(role.getRoleType()), Role::getRoleType, role.getRoleType())
              .eq(role.getStatus() != null, Role::getStatus, role.getStatus())
              .orderByAsc(Role::getSort);
        return this.page(page, wrapper);
    }

    @Override
    public boolean createRole(Role role) {
        return this.save(role);
    }

    @Override
    public boolean updateRole(Role role) {
        return this.updateById(role);
    }

    @Override
    public boolean deleteRole(Long id) {
        return this.removeById(id);
    }
}